<template>
  <!-- 会员管理 -->
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input v-model="form.username" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机号">
              <el-input v-model="form.phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="addFn">新增</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <el-table-column
          fixed
          type="index"
          label="编号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="姓名" prop="username" align="center" />
        <el-table-column label="手机号" prop="phone" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.phone |phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="created_at" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.created_at | dateFn}}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <!-- 尖刀连用户新增 -->
      <el-dialog
        title="尖刀连用户新增"
        :visible.sync="dialogVisible"
        :before-close="handleClose"
        width="468px"
      >
        <!-- 输入框 -->
        <el-form
          ref="addForm"
          :inline="true"
          label-width="80px"
          :rules="rules1"
          :model="addForm"
          label-position="right"
        >
          <el-form-item label="姓名" prop="username">
            <el-input v-model="addForm.username" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="addForm.phone" placeholder="请输入用户手机号" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose()">取 消</el-button>
          <el-button type="primary" @click="addSubmit()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import commonFn from "@/utils/common";
import {
  knifeUser,
  knifeUserAdd
} from "@/api/goddessMange";
export default {
  name: "knifeUser",
  data() {
    // 手机号校验
    var validatePhone = (rule, value, callback) => {
      const isRate = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/;
      if (!value) {
        // alert('空')
        callback(new Error("请输入手机号"));
      } else if (!isRate.test(value)) {
        callback(new Error("请输入正确手机号"));
      } else {
        callback();
      }
    };
    return {
      form: {
        username: "",
        phone: "",
        page: 1,
        pagesize: 10
      },
      tableData: [],
      // 分页
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0, // 总条目数,
      tableLoading: true,
      dialogFormVisible: false,
      tableData1: [],
      tableLoading1: true,
      dialogVisible: false,
      addForm: {
        username: null,
        phone: null
      },
      rules1: {
        username: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phone: [
          { validator:validatePhone,required: true, message: "请输入用户手机号", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList(this.form);
  },
  filters: {
    // 时间戳转化
    dateFn(e) {
      if (e != 0) {
        return new Date(Number(e) * 1000).format("yyyy-MM-dd hh:mm:ss");
      } else {
        return "--";
      }
    }
  },
  inject:['reload'],
  methods: {
    // 弹窗关闭
    handleClose() {
      this.dialogVisible = false;
    },
    // 添加提交
    addSubmit() {
      this.$refs["addForm"].validate(valid => {
        if (valid) {
            let datas = this.addForm
            knifeUserAdd(datas).then(res=>{
                console.log(res)
                if(res.data.code==200){
                    this.succStatus(res.data.msg)
                }else{
                    this.$message.error(res.data.msg)
                }
                this.reload()
            })
            .catch(error=>{
                this.$message.error(error)
                this.reload()
            })
            this.dialogVisible = false
        }else{
            this.$message({
                message: '必填项不能为空',
                type: 'warning'
            });
        }
      });
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    },
    // 添加用户
    addFn() {
      this.addForm.username = "";
      this.addForm.phone = "";
      this.dialogVisible = true;
    },
    // 序号
    table_index(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1;
    },
    // 列表
    getList(e) {
      this.tableLoading = true;
      knifeUser(e)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data.data;
            this.page_totals = res.data.data.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      console.log("this.form",this.form)
      this.getList(this.form);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
</style>
